package com.boatAnchorAssistant.service.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boatAnchorAssistant.pojo.entity.TreeNode;
import com.boatAnchorAssistant.service.mapper.TreeNodeMapper;
import com.boatAnchorAssistant.service.repository.in.TreeNodeRepository;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@Service
public class TreeNodeRepositoryImpl extends Repository<TreeNodeMapper, TreeNode> implements TreeNodeRepository {

    private final TreeNodeMapper mapper;

    public TreeNodeRepositoryImpl(TreeNodeMapper mapper) {
        this.mapper = mapper;
    }


    /**
     * 根据子代获取所有上级节点
     *
     * @param descendant
     * @return
     */
    @Override
    public List<TreeNode> getProgenitorTreeNodes(Long descendant) {
        if (StringUtils.isEmpty(descendant)){
            return Collections.EMPTY_LIST;
        }

        QueryWrapper<TreeNode> wrapper = getWrapper();
        wrapper.eq("is_delete",Boolean.FALSE);
        wrapper.eq("descendant",descendant);

        return mapper.selectList(wrapper);
    }

    /**
     * 获取所有后代id
     *
     * @param progenitor
     * @return
     */
    @Override
    public List<Long> getDescendantIds(Long progenitor) {
        return mapper.getDescendantIds(progenitor);
    }

    /**
     * 删除全部后代
     *
     * @param progenitor
     */
    @Override
    public void deleteDescendant(Long progenitor) {
        QueryWrapper<TreeNode> wrapper = getWrapper();
        wrapper.eq("is_delete",Boolean.FALSE);
        wrapper.eq("progenitor",progenitor);
        mapper.delete(wrapper);
    }
}